<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="xbsj-labels" content="Earth示例">
    </meta>
    <title>漫游-第一人称</title>
    <!-- 0 引入js文件：XbsjEarth.js和vue.min.js -->
    <script src="../../XbsjEarth/XbsjEarth.js"></script>
    <script src="./scripts/vue.min.js"></script>
    <style>
        html,
        body {
            width: 100%;
            height: 100%;
            margin: 0px;
            padding: 0px;
        }

        .defultbtn {
            display: inline-block;
            text-align: center;
            min-width: 60px;
            height: 38px;
            padding: 0 10px;
            line-height: 38px;
            border-radius: 100px;
            border: 1px solid #C9C9C9;
            background-color: #FFF;
            color: #555;
            cursor: pointer;
        }

        /* .defultbtn:hover {
            background-color: #b3daf8;
        } */

        .btnon {
            background-color: #1E9FFF;
            color: #fff;
            border: 1px solid #1E9FFF;
        }
    </style>
</head>

<body>
    <div id="vueApp" style="width: 100%; height: 100%; background: grey; position: relative;">
        <earth-comp></earth-comp>
    </div>

    <script>
        // 1 创建Earth的vue组件
        var EarthComp = {
            template: `
                <div style="width: 100%; height: 100%">
                    <div ref="earthContainer" style="width: 100%; height: 100%">
                    </div>
                    <div class="box" style="position: absolute; left: 18px; top: 18px; color: white; background: rgba(0, 0, 0, 0.6); padding: 20px; border-radius: 25px;min-width:200px; font-size:24px; font-family: 宋体;">
                        <div class="defultbtn" @click="ghostMode = !ghostMode">浸入模式</div>
                        <div class="defultbtn" :class="{'btnon':enabled}" @click="enabled = !enabled">键盘漫游</div>
                        <div class="defultbtn" @click="keynoteHelpShow = !keynoteHelpShow">按键说明</div><br/>
                        <div v-show="keynoteHelpShow" style="margin-top:10px">
                            <table border="1" cellpadding="0" cellspacing="0">
                                <tr>
                                    <th colspan="2">键盘按键说明</th>
                                </tr>
                                <tr v-for="(value,index) in shortcutkeys" :key="index">
                                    <td>{{value.key}}</td>
                                    <td>{{value.value}}</td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            `,
            data() {
                return {
                    _earth: undefined, // 注意：Earth和Cesium的相关变量放在vue中，必须使用下划线作为前缀！
                    ghostMode: false, // 控制浸入模式的开启与关闭，默认为关闭
                    enabled: true, // 控制键盘漫游的开启与关闭，默认为开启
                    keynoteHelpShow: false,
                    shortcutkeys: [
                        {
                            key: "W(↑)",
                            value: "水平方向前进"
                        },
                        {
                            key: "A(←)",
                            value: "水平方向左移"
                        },
                        {
                            key: "S(↓)",
                            value: "水平方向后退"
                        },
                        {
                            key: "D(→)",
                            value: "水平方向右移"
                        },
                        {
                            key: "SHIFT + W(↑)",
                            value: "视线方向前进"
                        },
                        {
                            key: "SHIFT + S(↓)",
                            value: "视线方向后退"
                        },
                        {
                            key: "Q",
                            value: "向左旋转"
                        },
                        {
                            key: "E",
                            value: "向右旋转"
                        },
                        {
                            key: "R",
                            value: "视点抬高"
                        },
                        {
                            key: "F",
                            value: "视点降低"
                        },
                        {
                            key: "1",
                            value: "减速（默认速度的基础上*0.8）"
                        },
                        {
                            key: "2",
                            value: "恢复默认速度"
                        },
                        {
                            key: "3",
                            value: "提速（默认速度的基础上*1.2）"
                        },
                        {
                            key: "4",
                            value: "切换 固定速度模式/自动速度模式",
                        }
                    ]
                };
            },
            // 1.1 资源创建
            mounted() {
                // 1.1.1 创建地球
                var earth = new XE.Earth(this.$refs.earthContainer);

                // 1.1.2 场景配置
                earth.sceneTree.root = {
                    "expand": true,
                    "title": "预览场景",
                    "children": [{
                        "ref": 'tileset',
                        "czmObject": {
                            "name": '大雁塔',
                            "xbsjType": "Tileset",
                            "url": '../assets/dayanta/tileset.json',
                            "xbsjUseOriginTransform": false,
                            "skipLevelOfDetail": false
                        },
                    }, {
                        "czmObject": {
                            "name": '默认影像',
                            "xbsjType": "Imagery",
                            "xbsjImageryProvider": XE.Obj.Imagery.defaultImageryProviderConfig,
                        },
                    }]
                };

                var tileset = earth.sceneTree.$refs.tileset.czmObject;

                // 飞入大雁塔
                tileset.flyTo();

                // 1.1.3 数据绑定
                // 双向绑定的属性，如果一方发生变化，另一方同样会跟随变化
                this._ghostModeUnbind = XE.MVVM.bind(this, 'ghostMode', earth.camera.immersion, 'ghostMode');
                this._enabledUnbind = XE.MVVM.bind(this, 'enabled', earth.camera.immersion, 'enabled');

                this._earth = earth;

                // only for Debug
                window.tileset = tileset;
                window.earth = earth;
            },
            filters: {
                numFilter(value) {
                    // 截取当前数据到小数点后两位
                    let realVal = parseFloat(value).toFixed(2)
                    return realVal
                }
            },
            // 1.2 资源销毁
            beforeDestroy() {
                // vue程序销毁时，需要清理相关资源
                this._ghostModeUnbind = this._ghostModeUnbind && this._ghostModeUnbind();
                this._enabledUnbind = this._enabledUnbind && this._enabledUnbind();
                this._earth = this._earth && this._earth.destroy();
            },
        }

        // 2 创建vue程序
        // XE.ready()用来加载Cesium.js等相关资源
        XE.ready().then(() => {
            var app = new Vue({
                el: '#vueApp',
                components: {
                    EarthComp,
                },
            });
        });
    </script>
</body>

</html>